package pool.bean;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import control.RequestParameter;
import dbmanager.DBManager;

import pool.tablename.Td_property_details;
import pool.tablename.Transaction_details;
import utility.ConvertToLower;


public class Transaction_details_Bean extends SubBean
{
	private Vector<Object> TransDetailVector = new Vector<Object>();
	private Vector<Object> propObjectVector = new Vector<Object>();
	
	private String tableName = "transaction_details";
	private String tdpl_tableName = "td_property_details";
	
	private Hashtable<Object,Object> newTransactionHash;
	private Hashtable<Object,Object> oldTransactionHash;
	private String tid;
	Transaction_details objTransDetails;
	
	
	private Vector<Object> OldTransDetailVector  = new Vector<Object>();
	private Vector<Object> NewTransDetailVector = new Vector<Object>();	
	 
	private Hashtable<Object,Object> ViewHashtable = new Hashtable<Object, Object>();
	
	public Transaction_details_Bean() {
		// TODO Auto-generated constructor stub
	}
	
	public void setId(String id)
	{
		this.tid = id ;
	}
	
	public void setTid(String tid)
	{
		this.tid = tid;
	}
	
		
	/**
	 * @param ObjectVector  is vector of properties of trnasction_details.  
	 * 
	 */
	public void setPropertyVector(Hashtable<Object,Object> PropertyHash)
	{
		Hashtable<Object,Object> propertyHash = new Hashtable<Object, Object>();
		propertyHash.putAll(PropertyHash);
		propertyHash = gin.convertMasterId(propertyHash, tableName,process);
		TransDetailVector = new Vector<Object>();
		TransDetailVector.add(propertyHash);
		for(int i=0;i<TransDetailVector.size();i++)
		 {
			 propObjectVector.add((Hashtable<Object, Object>)TransDetailVector.get(i)); 			 
		 }
	}
	
	
	/**
	 * @param ObjectVector  is vector of properties of transaction_details.  
	 * 
	 */
	public void setPropertyVector(Vector<Object> vector)
	{
		for(Object object : vector)
		{
			Hashtable<Object,Object> keyHash = new Hashtable<Object, Object>();
			Vector<Object> valueVector = new Vector<Object>() ;
			
			Hashtable<Object,Object> hashtable = new Hashtable<Object, Object>(); 
			hashtable.putAll((Hashtable<Object, Object>)object);
			Enumeration< Object> enumt = hashtable.keys();
			
			keyHash.putAll((Hashtable<Object, Object>)enumt.nextElement());
			valueVector.addAll((Vector<Object>) hashtable.get(keyHash));
			
			setPropertyVector(keyHash,valueVector);
		}
	}
	
	
	/**
	 * @param ObjectVector  is vector of properties of transaction_details.  
	 * 
	 */
	@SuppressWarnings("unchecked")
	public void setPropertyVector(Hashtable<Object,Object> TdMap,Vector<Object> TdplVector)
	{
		Hashtable<Object, Object> TransactionDeatilMap = new Hashtable<Object, Object>();
		 Hashtable<Object,Object> temp = new Hashtable<Object, Object>();
		 temp.putAll(TdMap);
		
		 temp.putAll(gin.convertMasterId(temp, tableName,process));
		 Vector<Object> tdplvector = new Vector<Object>();
		 
		for(Object object : TdplVector)
		{
			Hashtable<Object,Object> hashtable = new Hashtable<Object, Object>();
			hashtable.putAll((Hashtable<Object, Object>)object);
			Hashtable<Object, Object> PropertyMap1 = new Hashtable<Object, Object>();
			PropertyMap1.putAll(hashtable);
			PropertyMap1.putAll(gin.ConvertPVtoVT(PropertyMap1));
			PropertyMap1.putAll(gin.convertMasterId(PropertyMap1, tdpl_tableName,process));
						 //pl.tdpd.TdPropertyList.add(transobject);
			 tdplvector.add(PropertyMap1);
		}
		TransactionDeatilMap.put(temp,tdplvector);
		 propObjectVector.add(TransactionDeatilMap);		
	}	
		
	
	/**
	 * 
	 *insert into Transaction_details. 
	 */
	public StringBuffer insert()
	{
		StringBuffer sbrDTB = new StringBuffer();
		
		 sqlDB.setBeforeCommit();
		 
		objTransDetails=new Transaction_details(sqlDB);
		objTransDetails.setDirectRequestflag(true);
		objTransDetails.setTid(tid);
		objTransDetails.setPropertyVector(propObjectVector);
		objTransDetails.setRequestParameter(reqParam);

		objTransDetails.setTransactionMap();
		Hashtable<Object, Object> Error = new Hashtable<Object, Object>();
		Error.putAll(objTransDetails.process());
		if(Error.containsKey("ErrorInput"))
		{
			return new StringBuffer(Error.toString());
		}
		
		sbrDTB.append(objTransDetails.getInsert());
		
		boolean flag = objTransDetails.getResult();
		sqlDB.setAfterCommit(flag);
		System.out.println(sbrDTB.toString());
	
		return sbrDTB;
	}
	
	
	/**
	 * 
	 * @param oldPropertyHash is Hashtable of values to update into  transaction_details     
	 */
	public void setOldPropertyHash(Hashtable<Object,Object> oldPropertyHash)
	{
		objTransDetails=new Transaction_details(sqlDB);
		objTransDetails.setRequestParameter(reqParam);
		oldTransactionHash = new Hashtable<Object, Object>();
		oldTransactionHash.putAll(oldPropertyHash);
		oldTransactionHash=ConvertToLower.convertHashKey(oldTransactionHash);
		oldTransactionHash = gin.convertMasterId(oldTransactionHash, tableName,process);
		tid=oldTransactionHash.get("tid").toString();
		
		OldTransDetailVector.add(oldTransactionHash);
		
	}
	
	
	/**
	 * 
	 * @param newPropertyHash is Hashtable  of new property to update existing record of transaction_details.
	 */
	public void setNewPropertyHash(Hashtable<Object,Object> newPropertyHash)
	{
		objTransDetails=new Transaction_details(sqlDB);
		objTransDetails.setRequestParameter(reqParam);
		newTransactionHash = new Hashtable<Object, Object>();
		newTransactionHash.putAll(newPropertyHash);
		newTransactionHash=ConvertToLower.convertHashKey(newTransactionHash);
		newTransactionHash = gin.convertMasterId(newTransactionHash, tableName,process);
		
		NewTransDetailVector.add(newTransactionHash);		
	}
		
	
	/**
	 * this function converts old properties of vector in Transaction_details class format to update
	 * @param vector containing old properties
	 */
	@SuppressWarnings("unchecked")
	public void setOldPropertyVector(Vector<Object> vector)
	{
		for(Object object : vector)
		{
			Hashtable<Object,Object> hasht = (Hashtable<Object, Object>)object;
			setOldPropertyHash(hasht);
		}
	
	}
	
	
	/**
	 * this function converts new properties of vector in Transaction_details class format to update
	 * @param vector containing new properties
	 */
	@SuppressWarnings("unchecked")
	public void setNewPropertyVector(Vector<Object> vector)
	{
		for(Object object : vector)
		{
			Hashtable<Object,Object> hasht = (Hashtable<Object, Object>)object;
			setNewPropertyHash(hasht);
		}
	
	}
	
	
	/**
	 * Update  Transaction_details
	 * @return stringbuffer containing details of updation
	 */
	public StringBuffer update()
	{
		StringBuffer sbrDTB = new StringBuffer();
		
		 sqlDB.setBeforeCommit();
		 
		objTransDetails = new Transaction_details(sqlDB);
		objTransDetails.setRequestParameter(reqParam);
		if(!OldTransDetailVector.isEmpty())
			objTransDetails.setOldPropertyVector(OldTransDetailVector);
		if(!NewTransDetailVector.isEmpty())
			objTransDetails.setNewPropertyVector(NewTransDetailVector);
		objTransDetails.directRequest=true;
		objTransDetails.setTid(tid);
		objTransDetails.setTransactionMap();
		Hashtable<Object, Object> Error = new Hashtable<Object, Object>();
		Error.putAll(objTransDetails.process());
		if(Error.containsKey("ErrorInput"))
		{
			return new StringBuffer(Error.toString());
		}
		sbrDTB.append(objTransDetails.getUpdate());
		
		boolean flag = objTransDetails.getResult();
		sqlDB.setAfterCommit(flag);
		
		System.out.println(sbrDTB.toString());	
		return sbrDTB;
	}
	
	
	/**
	 * this function converts hashtable to Transaction_details class format for view
	 * @param hashtable contains property
	 */
	public void setViewHashtable(Hashtable<Object,Object> hashtable) 
	{

		Hashtable<Object,Object> PropertyHash = new Hashtable<Object, Object>();
		
		PropertyHash.putAll(hashtable);
		//PropertyHash = gin.ConvertPVtoVT(PropertyHash);
		PropertyHash.putAll(gin.getHashMapOfValues(PropertyHash));
		PropertyHash.putAll(ConvertToLower.convertHashKey(PropertyHash));
		PropertyHash = gin.convertMasterId(PropertyHash, tableName,process);	
		PropertyHash.putAll(gin.getHashMapOfValues(PropertyHash));	
		
		ViewHashtable.putAll(PropertyHash);
	}
	
	
	/**
	 * view() calls getView() of Transaction_details class
	 * @return stringbuffer containing details of Transaction_details in message format
	 */	
	public StringBuffer view()
	{
		 StringBuffer sbrDTB = new StringBuffer();
		 
		objTransDetails = new Transaction_details(sqlDB);
		objTransDetails.setRequestParameter(reqParam);
		objTransDetails.setViewHashtable(ViewHashtable);
	 
		sbrDTB.append(objTransDetails.getView());		 
	
		return sbrDTB;
	}
	
	
	public Vector<Object> getPropertyVector() 
	{
		return propObjectVector;
	}

	@Override
	public StringBuffer delete() {
		// TODO Auto-generated method stub
		return null;
	}
	
	
	
	
	/*public void setobjTransDetails(Transaction_details td)
	{
		ExecutionTimer t = new ExecutionTimer();
		 t.start();
		this.objTransDetails=td;
		t.end();
		@SuppressWarnings("unused")
		TimerRecordFile timerFile=new TimerRecordFile("pool.bean","Transaction_details_Bean","setobjTransDetails",t.duration());
		
	}
	public void setTransactionMap(Hashtable<Object, Object> oldTransactionHash)
	{
		ExecutionTimer t = new ExecutionTimer();
		 t.start();
		this.TransactionMap=oldTransactionHash;
		t.end();
		@SuppressWarnings("unused")
		TimerRecordFile timerFile=new TimerRecordFile("pool.bean","Transaction_details_Bean","setTransactionMap",t.duration());
		
		
	}*/
	public static void main(String[] args) 
	{
		Transaction_details_Bean prop = new Transaction_details_Bean();
		DBManager db = new DBManager();
		db.getConnect();
		prop.setDBManager(db);
		Hashtable<Object, Object> old = new Hashtable<Object, Object>();
		old.put("tid", "1");
		//old.put("item", "adh");
		//old.put("qty", "10");
		//old.put("pkg", "1");
		Hashtable<Object, Object> newHash = new Hashtable<Object, Object>();
		newHash.put("tid","1587");
		newHash.put("item","adh");
		newHash.put("qty", "10");
		newHash.put("pkg", "3");
		
		RequestParameter req = new RequestParameter();
		req.setUsername("+911234567891");
		req.setMessage("System Message");
		req.setPassword("891");
		req.setRequest("transaction_details");
		req.setProcess("view");
		
		
		
		prop.setReqParam(req);
		prop.setReqParam(req);
		
	
		//prop.setOldPropertyHash(old);
		//prop.setNewPropertyHash(newHash);
		//prop.setPropertyVector(old);
		prop.setViewHashtable(old);
		//prop.insertTransaction_details();
		prop.view();
		prop.insert();
		db.closeConnection();
	}

}
